package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.MyCollectionItemVo;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.MContants;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

/**
 * @author wxy
 * @createdate 2014-6-24 下午4:59:17
 * @Description: 我的收藏
 */
public class MyCollectionAdapter extends BaseAdapter {

    private ArrayList<MyCollectionItemVo> collectionDataVo;
    private Context context;
    private String type;// 根据type来判断显示哪个item
    private FinalBitmap fb;

    public MyCollectionAdapter(ArrayList<MyCollectionItemVo> collectionDataVo, Context context, String type) {
        super();
        this.collectionDataVo = collectionDataVo;
        this.context = context;
        this.type = type;

    }

    @Override
    public int getCount() {
        // Auto-generated method stub
        return collectionDataVo.size();
    }

    @Override
    public Object getItem(int position) {
        return collectionDataVo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        // 每个item的显示的内容，这个viewholder将所有的type的属性都包括在内

        TextView my_collection_item_new_title;// 新闻的标题
        TextView my_collection_item_new_time;// 新闻的时间
        TextView my_collection_item_new_introduce;// 新闻的简介

        TextView my_collection_item_column_name;// 栏目的标题
        TextView my_collection_item_column_hoster;// 栏目的主持人
        TextView my_collection_item_column_date;// 栏目的日期
        TextView my_collection_item_column_play_time;// 栏目的播出时间

        ImageView my_collection_item_new_img;// 新闻的图片
        ImageView my_collection_item_column_img;// 栏目的图片

    }

    ViewHolder viewHolder = null;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            viewHolder = new ViewHolder();
            // 根据不同的type显示不同的item布局
            switch (Integer.parseInt(type)) {
                case 1:
                case 3:
                    convertView = LayoutInflater.from(context).inflate(R.layout.my_collection_news_item, null);
                    viewHolder.my_collection_item_new_title = (TextView) convertView.findViewById(R.id.my_collection_item_new_title);
                    viewHolder.my_collection_item_new_time = (TextView) convertView.findViewById(R.id.my_collection_item_new_time);
                    viewHolder.my_collection_item_new_introduce = (TextView) convertView.findViewById(R.id.my_collection_item_new_introduce);
                    viewHolder.my_collection_item_new_img = (ImageView) convertView.findViewById(R.id.my_collection_item_new_img);
                    break;
                case 4:
                    convertView = LayoutInflater.from(context).inflate(R.layout.my_collection_column_item, null);
                    viewHolder.my_collection_item_column_name = (TextView) convertView.findViewById(R.id.my_collection_item_column_name);
                    viewHolder.my_collection_item_column_hoster = (TextView) convertView.findViewById(R.id.my_collection_item_column_hoster);
                    viewHolder.my_collection_item_column_date = (TextView) convertView.findViewById(R.id.my_collection_item_column_date);
                    viewHolder.my_collection_item_column_play_time = (TextView) convertView
                            .findViewById(R.id.my_collection_item_column_play_time);
                    viewHolder.my_collection_item_column_img = (ImageView) convertView.findViewById(R.id.my_collection_item_column_img);
                    break;
                default:
                    break;
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 根据不同的type从collectionDataVo中显示不同的数据 收藏类型 1娱乐 2美食 3新闻 4栏目 5安居
        String col_time = "";
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_activity_item_img);
        switch (Integer.parseInt(type)) {
            case 3:
                viewHolder.my_collection_item_new_title.setText(collectionDataVo.get(position).getTitle());

                // 对返回的时间做处理
//                col_time = collectionDataVo.get(position).getCol_time().substring(0, col_time_index);
//                viewHolder.my_collection_item_new_time.setText(col_time);

                viewHolder.my_collection_item_new_introduce.setText(collectionDataVo.get(position).getIntro());
                if (!MContants.EMPTY_STRING.equals(collectionDataVo.get(position).getImg_url())
                        || collectionDataVo.get(position).getImg_url() != null) {
                    fb = FinalBitmap.create(context);
                    fb.display(viewHolder.my_collection_item_new_img, ConnectionUtil.localUrl + collectionDataVo.get(position).getImg_url());
                    viewHolder.my_collection_item_new_img.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.my_collection_item_new_img.setVisibility(View.GONE);
                }
                break;
            case 4:
                viewHolder.my_collection_item_column_name.setText(collectionDataVo.get(position).getName());
                viewHolder.my_collection_item_column_hoster.setText(collectionDataVo.get(position).getHost());
                // 对返回的时间做处理
                int col_time_index = collectionDataVo.get(position).getCol_time().indexOf(" ");
                col_time = collectionDataVo.get(position).getCol_time().substring(0, col_time_index);
                viewHolder.my_collection_item_column_date.setText(col_time);

                viewHolder.my_collection_item_column_play_time.setText(collectionDataVo.get(position).getPlay_time());
                fb = FinalBitmap.create(context);
                if (!MContants.EMPTY_STRING.equals(collectionDataVo.get(position).getImg_url())
                        || collectionDataVo.get(position).getImg_url() != null) {
                    fb.display(viewHolder.my_collection_item_column_img, ConnectionUtil.localUrl + collectionDataVo.get(position).getImg_url());
                } else {
                    // viewHolder.my_collection_item_column_img.setBackgroundResource(R.drawable.my_activity_item_bg);
                    fb.display(viewHolder.my_collection_item_column_img, null, bitmap, bitmap);
                }
                break;

            default:
                break;
        }
        return convertView;
    }
}
